home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / gnustuff / tos / progut~1 / iostream.zoo / test / tfile.cc < prev    next >
Encoding:
C/C++ Source or Header  |  1991-09-22  |  6.4 KB  |  291 lines

  1. // This may look like C code, but it is really -*- C++ -*-
  2.  
  3. /*
  4.  * a few tests for streams
  5.  *
  6.  */
  7.  
  8. #include <stream.h>
  9. #include <fstream.h>
  10. #include "strstrea.h"
  11. //#include <SFile.h>
  12. //#include <PlotFile.h>
  13. #include <stdlib.h>
  14. #include <assert.h>
  15.  
  16. #define io_writeonly ios::out
  17.  
  18. class record
  19. {
  20. public:
  21.   char c; int i; double d;
  22. };
  23.  
  24. ostream& operator<<(ostream& s, record& r)
  25. {
  26.   return(s << "(i = " << r.i << " c = " << r.c << " d = " << r.d << ")");
  27. }
  28.  
  29. void t1()
  30. {
  31.   char ch;
  32.  
  33.   assert(cout.good());
  34.   assert(cout.writable());
  35.   assert(cout.is_open());
  36.   cout << "Hello, world via cout\n";
  37.   assert(cerr.good());
  38.   assert(cerr.writable());
  39.   assert(cerr.is_open());
  40.   cerr << "Hello, world via cerr\n";
  41.  
  42.   assert(cin.good());
  43.   assert(cin.readable());
  44.   assert(cin.is_open());
  45.  
  46.   cout << "enter a char:";  cin >> ch;
  47.   cout.put('c');  cout.put(' ');  cout.put('=');  cout.put(' ');
  48.   cout.put('"');  cout.put(ch);    cout << '"';  cout << char('\n');
  49.   assert(cin.good());
  50.   assert(cout.good());
  51. }
  52.  
  53. void t2()
  54. {
  55.   int i;
  56.   short h;
  57.   long l;
  58.   float f;
  59.   double d;
  60.   char s[100];
  61.  
  62.   cout << "enter three integers (short, int, long):";  
  63.   cin >> h; cin >> i;   
  64.   // cin.scan("%ld", &l);
  65.   cin >> l;
  66.   cout << "first  = " << h << " via dec = " << dec(h, 8) << "\n";
  67.   cout << "second = " << i << form(" via form = %d = 0%o", i, i);
  68.   cout.form(" via cout.form = %d = 0x%x\n", i, i);
  69.   cout << "third  = " << l  << " via hex = " << hex(l) << "\n";
  70.   assert(cin.good());
  71.   assert(cout.good());
  72.  
  73.   cout << "enter a float then a double:";  cin >> f; cin >> d;
  74.   cout << "first  = " << f << "\n";
  75.   cout << "second = " << d << "\n";
  76.   assert(cin.good());
  77.   assert(cout.good());
  78.  
  79.   cout << "enter 5 characters separated with spaces:";
  80.   cin >> s;
  81.   cout << "first  = " << s << "\n";
  82.   cin.get(s, 100);
  83.   cout << "rest   = " << s << "\n";
  84.  
  85.   assert(cin.good());
  86.   assert(cout.good());
  87.  
  88. }
  89.  
  90. void t3()
  91. {
  92.   char ch;
  93.   cout << "\nMaking streams sout and sin...";
  94.   ofstream sout("streamfile");
  95.   assert(sout.good());
  96.   assert(sout.is_open());
  97.   assert(sout.writable());
  98.   assert(!sout.readable());
  99.   sout << "This file has one line testing output streams.\n";
  100.   sout.close();
  101.   assert(!sout.is_open());
  102.   ifstream sin("streamfile");
  103.   assert(sin.good());
  104.   assert(sin.is_open());
  105.   assert(!sin.writable());
  106.   assert(sin.readable());
  107.   cout << "contents of file:\n";
  108.   while(sin >> ch) cout << ch;
  109.   sin.close();
  110.   assert(!sin.is_open());
  111. }
  112.  
  113.  
  114. #if 0
  115. void t4()
  116. {  
  117.   char s[100];
  118.   char ch;
  119.   int i;
  120.  
  121.   cout << "\nMaking File tf ... ";
  122.   File tf("tempfile", io_readwrite, a_create);
  123.   assert(tf.good());
  124.   assert(tf.is_open());
  125.   assert(tf.writable());
  126.   assert(tf.readable());
  127.   strcpy(s, "This is the first and only line of this file.\n");
  128.   tf.put(s);
  129.   tf.seek(0);
  130.   tf.get(s, 100);
  131.   assert(tf.good());
  132.   cout << "first line of file:\n" << s << "\n";
  133.   cout << "next char = ";
  134.   tf.get(ch);
  135.   cout << (int)ch;
  136.   cout.put('\n');
  137.   assert(ch == 10);
  138.   strcpy(s, "Now there is a second line.\n");
  139.   cout << "reopening tempfile, appending: " << s;
  140.   tf.open(tf.name(), io_appendonly, a_use);
  141.   assert(tf.good());
  142.   assert(tf.is_open());
  143.   assert(tf.writable());
  144.   assert(!tf.readable());
  145.   tf.put(s);
  146.   assert(tf.good());
  147.   tf.open(tf.name(), io_readonly, a_use);
  148.   tf.raw();
  149.   assert(tf.good());
  150.   assert(tf.is_open());
  151.   assert(!tf.writable());
  152.   assert(tf.readable());
  153.   cout << "First 10 chars via raw system read after reopen for input:\n";
  154.   read(tf.filedesc(), s, 10);
  155.   assert(tf.good());
  156.   for (i = 0; i < 10; ++ i)
  157.     cout.put(s[i]);
  158.   lseek(tf.filedesc(), 5, 0);
  159.   cout << "\nContents after raw lseek to pos 5:\n";
  160.   while ( (tf.get(ch)) && (cout.put(ch)) );
  161.   tf.remove();
  162.   assert(!tf.is_open());
  163. }
  164.  
  165. void t5()
  166. {
  167.   record r;
  168.   int i;
  169.   cout << "\nMaking SFile rf...";
  170.   SFile rf("recfile", sizeof(record), io_readwrite, a_create);
  171.   assert(rf.good());
  172.   assert(rf.is_open());
  173.   assert(rf.writable());
  174.   assert(rf.readable());
  175.   for (i = 0; i < 10; ++i)
  176.   {
  177.     r.c = i + 'a';
  178.     r.i = i;
  179.     r.d = (double)(i) / 1000.0;
  180.     rf.put(&r);
  181.   }
  182.   assert(rf.good());
  183.   cout << "odd elements of file in reverse order:\n";
  184.   for (i = 9; i >= 0; i -= 2)
  185.   {
  186.     rf[i].get(&r);
  187.     assert(r.c == i + 'a');
  188.     assert(r.i == i);
  189.     cout << r << "\n";
  190.   }
  191.   assert(rf.good());
  192.   rf.remove();
  193.   assert(!rf.is_open());
  194. }
  195.  
  196. void t6()
  197. {
  198.   cout << "\nMaking PlotFile pf ..."; 
  199.   PlotFile pf("plot.out", io_writeonly, a_create);
  200.   assert(pf.good());
  201.   assert(pf.is_open());
  202.   assert(pf.writable());
  203.   assert(!pf.readable());
  204.   pf.move(10,10);
  205.   pf.label("Test");
  206.   pf.circle(300,300,200);
  207.   pf.line(100, 100, 500, 500);
  208.   assert(pf.good());
  209.   cout << "(You may delete or attempt to plot " << pf.name() << ")\n";
  210. }
  211. #endif
  212.  
  213. void t7()
  214. {
  215.   char ch;
  216.   char mybuf[1000];
  217.   cout << "\ncreating string-based ostream...\n";
  218.   ostrstream strout(mybuf, 1000);
  219.   assert(strout.good());
  220.   assert(strout.writable());
  221.   strout << "This is a string-based stream.\n";
  222.   strout << "With two lines.\n";
  223.   strout.put(char(0));
  224.   assert(strout.good());
  225.   cout << "with contents:\n";
  226.   cout << mybuf;
  227.   cout << "using it to create string-based istream...\n";
  228.   istrstream strin(mybuf, strlen(mybuf));
  229.   assert(strin.good());
  230.   assert(strin.readable());
  231.   cout << "with contents:\n";
  232.   while (strin.get(ch)) cout.put(ch);
  233. }
  234.  
  235. #if 0
  236. void t8()
  237. {
  238.   cout << "\nThe following file open should generate error message:";
  239.   cout.flush();
  240.   File ef("shouldnotexist", io_readonly, a_useonly);
  241.   assert(!ef.good());
  242.   assert(!ef.is_open());
  243. }
  244. #endif
  245.  
  246. void t9()
  247. {
  248.   char ch;
  249.   cout << "\nMaking filebuf streams fout and fin...";
  250.   filebuf foutbuf;
  251.   foutbuf.open("ffile", ios::out);
  252.   ostream fout(&foutbuf);
  253.   assert(fout.good());
  254.   assert(fout.is_open());
  255.   assert(fout.writable());
  256.   assert(!fout.readable());
  257.   fout << "This file has one line testing output streams.\n";
  258.   fout.close();
  259.   assert(!fout.is_open());
  260.   filebuf finbuf;
  261.   finbuf.open("ffile", ios::in);
  262.   istream fin(&finbuf);
  263.   assert(fin.good());
  264.   assert(fin.is_open());
  265.   assert(!fin.writable());
  266.   assert(fin.readable());
  267.   cout << "contents of file:\n";
  268.   while(fin >> ch) cout << ch;
  269.   fin.close();
  270.   assert(!fin.is_open());
  271. }
  272.  
  273. main()
  274. {
  275.   t1();
  276.   t2();
  277.   t3();
  278. //  t4();
  279. //  t5();
  280. //  t6();
  281.   t7();
  282.   t9();
  283. //  t8(); 
  284.  
  285.   cout << "\nFinal names & states:\n";
  286.   cout << "cin:      " << /*cin.name()  << */ "\t" << cin.rdstate() << "\n";
  287.   cout << "cout:     " << /*cout.name() << */ "\t" << cout.rdstate() << "\n";
  288.   cout << "cerr:     " << /*cerr.name() << */ "\t" << cerr.rdstate() << "\n";
  289.   cout << "\nend of test.\n";
  290. }
  291.